/* Global workspace definition */

if(!window.console || !window.console.log){
	window.console = {};
	window.console.log = function(){};
}

define(['jquery', 'bxSlider'], function($, bx){

	var config = {
			js_modules : "modules/",
			css_modules : "/_layouts/Css/modules/",
			services : "/_layouts/Mockups/"
		},
		bxslider = null,
		$slider = null;



	/* Selectors */
	var selectors = {
		jumpTop : "a.jump-top"
	};

	function service(path, params, method, datatype){
		if(!method)
			method = "get"
		if(!datatype)
			datatype = 'json';
		return $.ajax({
			url: config.services + path,
			dataType: datatype,
			method: method,
			data : typeof params == "string" ?  params : JSON.stringify(params)
		});
	};

	function init(){

		/*
		$slider = $("#slider");

		bxslider = $slider.find('ul.slides').bxSlider({
			pagerCustom: "#slider .thumbs"
		});
		*/

		/* Bind UI events*/
		bindEvents();

		loadAllModules();

		// loadOverlays();

	};

	function createCustomRadios(section){
		var selectedClass = "checked";

		$(section).on("click", "ul[data-radio] > li", function(e){
			e.stopPropagation();
			$this = $(this);
			if(!$this.hasClass(selectedClass)){
				$this.addClass(selectedClass);
				$this.siblings('.' + selectedClass).removeClass(selectedClass);

				$this.parent().data('checked', $this.data('option'));
				$this.trigger('changed', $this.data('option'));
			}
		});
	}

	function loadModule(module, container){
		// Load stylesheets defined in attribute: data-styles
		var styles = $(container).data("styles"),
			loadedStyles = [],
			current_style, found,
			i, j;

		if(styles){
			styles = styles.split(" ");

			/* Get list of loaded stylesheets */
			$("head").find("link[rel='stylesheet']").each(function(){ loadedStyles.push(this.href) });

			for(i = 0; i < styles.length; i++){
				current_style = styles[i];
				found = false;
				for(j = 0; j < loadedStyles.length; j++){
					if(loadedStyles[j].match(current_style + ".css")){
						found = true;
						break;
					}
				}
				if(!found){
					loadCSS(current_style);
				}
			}
		}
		require([config.js_modules + module], function(m){
			m.init(container);
		})
	};

	function loadCSS(cssfile){
		var link = document.createElement("link");
		link.type = "text/css";
		link.rel = "stylesheet";
		link.href = config.css_modules + cssfile + ".css";
		document.getElementsByTagName("head")[0].appendChild(link);
	};

	function loadAllModules(){
		$("[data-module]").each(function(){
			var module = $(this).data('module');
			loadModule(module, this);
		});
	};

	function loadOverlays(){
		require(['jquery', 'vendor/jquery.simplemodal'], function($, modal){
			window.onSubscribeShareClick = function(btn) {
				if (btn == 3) {
					$('#lblSubscriptionResp').html('');
					$("#subscribe-modal").modal({ close: true, opacity: 90, overlayClose: true });
					$(".subscribe-module-modal").css("display", "inline");
				}
				else if (btn == 2) {
					var msg = 'I would like to share with you this page: ' + location.href;
					$('#txtBody').val(msg);
					$('#txtSubject').val('Share from Marlboro Branded Space');
					$("#share-modal").modal({ close: true, opacity: 90, overlayClose: true });
					$(".share-module-modal").css("display", "inline");
				}
			}
		});
	};

	/* Scrolls the page up */
	function goTop(e){

		if(e instanceof $.Event)
			e.preventDefault();
	};

	/* Binds UI events*/
	function bindEvents(){
		$(selectors.jumpTop).click( $.proxy(goTop, this) );
	};


	function getUrlParams(){
		var url = document.location.search,
			params = {}, tokens = [], tmp = [];

		if(url.length > 0){
			tokens = document.location.search.substring(1).split('&');
			for(var i = 0; i < tokens.length; i++){
				tmp = tokens[i].split('=');
				params[tmp[0]] = tmp[1];
			}
		}
		return params;
	}

	function getServicePath(){
		return config.services;
	}

	if(!window.subscriptionQueue){
		window.subscriptionQueue = {
			"events" : {}
		}
	}

	function subscribe(event, fun, config){
		console.log("subscribe event: " + event);
		var defaults = {
			"queue" : false
		}
		config = $.extend(defaults, config);

		$(document).on(event, function(data){
			console.log("Event " + event + " triggered");
			fun(data);
		});

		if(config.queue && window.subscriptionQueue.events[event]){
			console.log("Event queued " + event + " re-triggered");
			fun(window.subscriptionQueue.events[event]);
		}
	}

	function publish(event, data){
		window.subscriptionQueue.events[event] = data || true;
		var event = jQuery.Event(event);
		$(document).trigger(event, data);
	}

	/* Public functions and properties */
	return {
		"init" : init,
		"selectors" : selectors,
		"goTop" : goTop,
		"loadModule" : loadModule,
		"loadCSS" : loadCSS,
		"service" : service,
		"getUrlParams" : getUrlParams,
		"getServicePath" : getServicePath,
		"subscribe" : subscribe,
		"publish" : publish
	};
});


